
package com.booking.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.booking.R;

public class PlacesContainerFragment extends BasicFragment implements OnCheckedChangeListener {
    private BasicFragment mFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_places_container, container, false);
        RadioGroup group = (RadioGroup) view.findViewById(R.id.group);
        group.setOnCheckedChangeListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showMainFragment(FragmentType.PLACES_LIST);
    }

    private void showMainFragment(FragmentType type) {
        if (type == null) {
            return;
        }
        if (mFragment != null && mFragment.getFragmentType() == type) {
            return;
        }
        FragmentManager fragmentManager = getChildFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(type.name());
        if (fragment == null) {
            fragment = createFragment(type);
            if (fragment == null) {
                return;
            }
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (mFragment != null) {
                transaction.detach(mFragment);
            }
            mFragment = (BasicFragment) fragment;
            transaction.add(R.id.container_fragment, mFragment, mFragment.getFragmentType().name());
            transaction.commit();
        } else {
            BasicFragment basicFragment = (BasicFragment) fragment;
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (mFragment != null) {
                transaction.detach(mFragment);
            }
            mFragment = basicFragment;
            transaction.attach(mFragment);
            transaction.commit();
        }
    }

    private Fragment createFragment(FragmentType type) {
        if (type == null) {
            return null;
        }
        switch (type) {
            case MAP:
                return new MapFragment();
            case PLACES_LIST:
                return new PlacesFragment();
            case FILTER_FRAGMENT:
                return new FilterFragment();
            default:
                return null;
        }
    }

    @Override
    public void updateData() {
    }

    @Override
    public FragmentType getFragmentType() {
        return FragmentType.PLACES_CONTAINER;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentType fragmentType = null;
        switch (checkedId) {
            case R.id.tab_list:
                fragmentType = FragmentType.PLACES_LIST;
                break;
            case R.id.tab_filter:
                fragmentType = FragmentType.FILTER_FRAGMENT;
                break;
            case R.id.tab_map:
                fragmentType = FragmentType.MAP;
                break;
            default:
                break;
        }
        showMainFragment(fragmentType);
    }
}
